import { Injectable } from '@nestjs/common';
import { Cat } from './interface/cat.interface';
import { InjectRepository } from '@nestjs/typeorm';
import { Cats } from './cats.entity';
import { Repository } from 'typeorm';
import { CreateCats } from './dto/createCats.dto';
import { UpdateCats } from './dto/updateCats.dto';
import { DeleteById } from './dto/deleteById.dto';

@Injectable()
export class CatsService {

  constructor(
    @InjectRepository(Cats) private readonly catsRepository: Repository<Cats>,
  ) {}

  async findAll(): Promise<Cat[]> {
    return await this.catsRepository.find();
  }

  async findById(id: string): Promise<Cat[]> {
    return  await this.catsRepository.find({ where: { id } });
  }
  /**
   *
   * @param payload 添加一个Cats
   */
  async add(payload: CreateCats): Promise<Cats> {
    return await this.catsRepository.save(this.catsRepository.create(payload));
  }

  async update(payload: UpdateCats): Promise<Cats> {
    // const updateCats=this.catsRepository.create(payload);
    return await this.catsRepository.save(payload);
  }

  async delete(payload: DeleteById): Promise<void> {
    await this.catsRepository.delete(payload);
  }
}
